Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Python Functions → Scope of Variables

Python Functions

Scope of Variables

Python functions are blocks of reusable code that encapsulate specific tasks. A crucial aspect of understanding functions is grasping how variable scope works within them. Scope determines where a variable is accessible within your program. Python's scoping rules follow the LEGB rule:

LEGB Rule

⮞ Local: Variables defined inside a function are local to that function. They only exist and are accessible within the function's body ⮞ Enclosing function locals: If a variable is not found locally, Python searches the local scopes of any enclosing functions (nested functions) ⮞ Global: Variables defined outside any function are global. They are accessible from anywhere in the program after their definition ⮞ Built-in: These are pre-defined functions and constants available in Python (e.g., `len()`, `print()`, `True`)

Example 1: Local Scope

Python function local scope variable def my_function(): x = 10 # x is local to my_function print(f"Inside the function, x = {x}") my_function() print(x) # This will raise a NameError: name 'x' is not defined

Output

Inside the function, x = 10
Here, `x` is defined within `my_function()`, making it local. Trying to access `x` outside the function results in an error because it's not visible in the global scope.

Example 2: Global Scope

python function variable Global Scope y = 20 # y is a global variable def my_function(): print(f"Inside the function, y = {y}") my_function() print(f"Outside the function, y = {y}")

Output

Inside the function, y = 20 Outside the function, y = 20
`y` is defined globally. The function `my_function()` can access it because global variables are visible within functions (unless shadowed by a local variable with the same name).

Example 3: Enclosing Function Locals (Nested Functions)

Python - Enclosing Function Locals def outer_function(): z = 30 # z is local to outer_function def inner_function(): print(f"Inside inner function, z = {z}") inner_function() outer_function() #print(z) # This will raise a NameError: name 'z' is not defined

Output

Inside inner function, z = 30
`inner_function()` is nested within `outer_function()`. `inner_function()` can access `z` because it's in the enclosing function's scope. However, `z` is not accessible outside `outer_function()`.

Example 4: Modifying Global Variables (using `global`)

Modifying Global Variables (using `global`) in Python global_var = 40 def modify_global(): global global_var # Declare that we're modifying the global variable global_var = 50 modify_global() print(f"Global var after modification: {global_var}")

Output

Global var after modification: 50
To change a global variable from within a function, you must use the `global` keyword. Without it, the function would create a new local variable with the same name.

Example 5: `nonlocal` keyword (for enclosing function variables)

Python `nonlocal` keyword def outer(): enclosing_var = 60 def inner(): nonlocal enclosing_var # Access and modify enclosing variable enclosing_var = 70 inner() print(f"Enclosing var after inner function: {enclosing_var}") outer()

Output

70
The `nonlocal` keyword is used to modify a variable in an enclosing function's scope, without making it global.

Example 6: Shadowing

variable Shadowing in Python a = 100 # Global a def myfunc(): a = 5 # Local a - shadows the global a print(f"Inside function: a = {a}") myfunc() print(f"Outside function: a = {a}")

Output

Inside function: a = 5 Outside function: a = 100
The local `a` within `myfunc()` shadows the global `a`. The function uses its local `a`, leaving the global `a` unchanged.

Tutorials